package com.jzo2o.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.market.mapper.ActivityMapper;
import com.jzo2o.market.mapper.CouponMapper;
import com.jzo2o.market.model.domain.Activity;
import com.jzo2o.market.model.domain.Coupon;
import com.jzo2o.market.model.dto.request.ActivityQueryForPageReqDTO;
import com.jzo2o.market.model.dto.request.ActivitySaveReqDTO;
import com.jzo2o.market.model.dto.response.ActivityInfoResDTO;
import com.jzo2o.market.service.IActivityService;
import com.jzo2o.market.service.ICouponService;
import com.jzo2o.market.service.ICouponWriteOffService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-16
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {
    private static final int MILLION = 1000000;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ICouponService couponService;

    @Resource
    private ICouponWriteOffService couponWriteOffService;
    @Resource
    private ActivityMapper activityMapper;


    @Override
    public void saveORupdateActivity(ActivitySaveReqDTO activitySaveReqDTO) {
        if (ObjectUtils.isEmpty(activitySaveReqDTO.getId())) {
            Activity activity = BeanUtil.toBean(activitySaveReqDTO, Activity.class);
            activity.setStatus(1);
            boolean save = this.save(activity);
            if (!save){
                throw new BadRequestException("保存活动失败");
            }
        }
        if (ObjectUtils.isNotEmpty(activitySaveReqDTO.getId())) {

            this.lambdaUpdate().set(Activity::getStatus, 1)
                    .eq(Activity::getId, activitySaveReqDTO.getId())
                    .set(Activity::getName, activitySaveReqDTO.getName())
                    .set(Activity::getType, activitySaveReqDTO.getType())
                    .set(Activity::getDiscountAmount, activitySaveReqDTO.getDiscountAmount())
                    .set(Activity::getDiscountRate, activitySaveReqDTO.getDiscountRate())
                    .set(Activity::getAmountCondition, activitySaveReqDTO.getAmountCondition())
                    .set(Activity::getTotalNum, activitySaveReqDTO.getTotalNum())
                    .set(Activity::getValidityDays, activitySaveReqDTO.getValidityDays())
                    .set(Activity::getDistributeStartTime, activitySaveReqDTO.getDistributeStartTime())
                    .set(Activity::getDistributeEndTime, activitySaveReqDTO.getDistributeEndTime())
                    .update();

        }
    }

    @Override
    public PageResult<ActivityInfoResDTO> pagequery(ActivityQueryForPageReqDTO activityQueryForPageReqDTO) {
        LambdaQueryWrapper<Activity> wrapper = Wrappers.<Activity>lambdaQuery()
                .eq(activityQueryForPageReqDTO.getId() != null, Activity::getId, activityQueryForPageReqDTO.getId())
                .like(ObjectUtils.isNotNull(activityQueryForPageReqDTO.getName()), Activity::getName, activityQueryForPageReqDTO.getName())
                .eq(activityQueryForPageReqDTO.getStatus() != null, Activity::getStatus, activityQueryForPageReqDTO.getStatus())
                .eq(activityQueryForPageReqDTO.getType() != null, Activity::getType, activityQueryForPageReqDTO.getType())
                .orderByDesc(Activity::getCreateTime);
        Page<Activity> page = PageUtils.parsePageQuery(activityQueryForPageReqDTO, Activity.class);
        Page<Activity> activityPage = baseMapper.selectPage(page, wrapper);
        PageResult<ActivityInfoResDTO> objectPageResult = new PageResult<>();
        objectPageResult.setTotal(activityPage.getTotal());
        objectPageResult.setPages(activityPage.getPages());
        objectPageResult.setList(BeanUtil.copyToList(activityPage.getRecords(), ActivityInfoResDTO.class));

        return objectPageResult;
    }

    @Override
    public ActivityInfoResDTO selectById(Long id) {
        Activity activity = this.getById(id);
        if (ObjectUtils.isNotEmpty(activity)) {
            return BeanUtil.toBean(activity, ActivityInfoResDTO.class);
        }
       throw new BadRequestException("活动不存在");
    }
    @Resource
    private CouponMapper couponMapper;
    @Override
    public void revokeById(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new BadRequestException("活动不存在");
        }
        ActivityInfoResDTO activityInfoResDTO = this.selectById(id);
        if (activityInfoResDTO.getStatus() == 1 || activityInfoResDTO.getStatus() == 2){
            this.lambdaUpdate().eq(Activity::getId, id).set(Activity::getStatus, 4).update();
            List<Coupon> coupons = couponMapper.selectList(Wrappers.<Coupon>lambdaQuery().eq(Coupon::getActivityId, id));
            coupons.forEach(coupon -> {
                coupon.setStatus(3);
                couponService.updateById(coupon);
            });
        }

    }
}
